package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.service.TaskService;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lk
 * @date 2020/1/11 0011 21:12
 * @description: 任务中心
 */
@RestController
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    LoginService loginService;

    /**
     * 领取任务奖励
     *
     * @param token
     * @param t_id
     * @return
     */
    @RequestMapping(value = "/task/getaward", method = RequestMethod.POST)
    public String getAward(@CookieValue("token") String token,
                           @RequestParam(value = "t_id") String t_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = taskService.receiveAward(u_id, t_id);
            return result;
        }

    }

    /**
     * 获取我的任务列表
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/task/getmytask", method = RequestMethod.GET)
    public String getTask(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = taskService.getUserTask(u_id);
            return result;
        }

    }
}
